package cn.com.surker.workcon.service.fengdu;

import cn.com.surker.base.system.entity.SysLine;
import cn.com.surker.control.entity.ControlExpendBatch;
import cn.com.surker.enums.StateEnum;
import cn.com.surker.enums.system.IsAutoEnum;
import cn.com.surker.lab.entity.LabExpendBatch;
import cn.com.surker.util.DateUtil;
import cn.com.surker.util.StringUtils;
import cn.com.surker.workcon.common.GKJDBCService;
import cn.com.surker.workcon.service.PieceService;
import org.springframework.jdbc.core.JdbcTemplate;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 盘信息 Mapper 接口
 * </p>
 *
 * @author admin
 * @since 2024-09-26
 */
public class PieceServiceImpl extends GKJDBCService implements PieceService {

	/**
	 * 根据工控机的发货单,查询工控机的盘信息
	 *
	 * @param prodId   工控机的发货单id
	 * @param beSerial 起始盘号(第几盘,不包括该盘本身,可以为空)
	 * @return
	 */
	public List<LabExpendBatch> findByProdId(Long lineId, Long invoiceId, String prodId, Integer beSerial, Date createDate) {
		if (lineId == null || StringUtils.isBlank(prodId)) {
			return null;
		}
		SysLine line = getLine(lineId);
		if (line == null) {
			return null;
		}
		JdbcTemplate lineJdbcTemp = getLineJdbcTemp(line);
		if (lineJdbcTemp == null) {
			return null;
		}
		//	查询盘数据
		String queryBatchSql = "SELECT ID,HID,TrayNum,Volume,EleValue,IsManual,MixTime,CreateTime FROM HistoryTray where hid = ?";
		Object[] param = {prodId};
		List<Map<String, Object>> pieceList = lineJdbcTemp.queryForList(queryBatchSql, param);
		if (pieceList == null || pieceList.isEmpty()) {
			return null;
		}
		//	转换数据
		List<LabExpendBatch> batchList = new ArrayList<>();
		for (int i = 0, s = pieceList.size(); i < s; i++) {
			Map<String, Object> piece = pieceList.get(i);
			LabExpendBatch batch = piece2Batch(invoiceId, lineId, prodId, piece, createDate);
			batchList.add(batch);
		}
		return batchList;
	}

	@Override
	public List<ControlExpendBatch> findByProdId(Long lineId, Long erpInvId, String prodId, Date createDate) {
		SysLine line = getLine(lineId);
		if (line == null) {
			return null;
		}
		JdbcTemplate lineJdbcTemp = getLineJdbcTemp(line);
		if (lineJdbcTemp == null) {
			return null;
		}
		String queryBatchSql = "SELECT ID,HID,TrayNum,Volume,EleValue,IsManual,MixTime,CreateTime FROM HistoryTray where hid = ?";
		Object[] param = {prodId};
		List<Map<String, Object>> pieceList = lineJdbcTemp.queryForList(queryBatchSql, param);
		if (pieceList == null || pieceList.isEmpty()) {
			return null;
		}
		//	转换数据
		List<ControlExpendBatch> batchList = new ArrayList<>();
		for (int i = 0, s = pieceList.size(); i < s; i++) {
			Map<String, Object> piece = pieceList.get(i);
			ControlExpendBatch batch = piece2Batch2(lineId, piece, erpInvId, createDate);
			batchList.add(batch);
		}
		return batchList;
	}


	/**
	 * erp自己的发货单
	 *
	 * @param invoiceId
	 * @param lineId
	 * @param prodId
	 * @param piece
	 * @param createDate
	 * @return
	 */
	private LabExpendBatch piece2Batch(Long invoiceId, Long lineId, String prodId, Map<String, Object> piece, Date createDate) {
		if (invoiceId == null || lineId == null || piece == null) {
			return null;
		}
		//	工控机盘记录号
		String pieId = String.valueOf(piece.get("ID"));
		//	工控机盘号
		Integer serial = Integer.valueOf(String.valueOf(piece.get("TrayNum")));
		//	工控机盘方量
		Double pieAmnt = Double.valueOf(String.valueOf(piece.get("Volume")));
		//	工控机的本盘生产时间
		Date datTim = DateUtil.convertDate(String.valueOf(piece.get("CreateTime")));
		//	手动自动,默认自动
		Integer autoType = IsAutoEnum.ZD.getCode();
		String remark = "自动同步工控机";
		LabExpendBatch erpBatch = new LabExpendBatch();
		erpBatch.setInvoiceId(invoiceId);
		erpBatch.setLineId(lineId);
		erpBatch.setBatchStere(pieAmnt);
		erpBatch.setBatchTime(datTim);
		erpBatch.setConBatchId(pieId);
		erpBatch.setConBatchNum(serial);
		erpBatch.setType(autoType);
		erpBatch.setRemark(remark);
		erpBatch.setCreator(bcsCreater);
		erpBatch.setCreateTime(createDate);
		return erpBatch;
	}

	private ControlExpendBatch piece2Batch2(Long lineId, Map<String, Object> piece, Long erpInvId, Date createDate) {
		if (lineId == null || piece == null) {
			return null;
		}
		//	工控机盘记录号
		String pieId = String.valueOf(piece.get("ID"));
		//	工控机盘号
		Integer serial = Integer.valueOf(String.valueOf(piece.get("TrayNum")));
		//	工控机盘方量
		Double pieAmnt = Double.valueOf(String.valueOf(piece.get("Volume")));
		//	工控机的本盘生产时间
		Date datTim = DateUtil.convertDate(String.valueOf(piece.get("CreateTime")));
		//	手动自动,默认自动
		Integer autoType = IsAutoEnum.ZD.getCode();
		String remark = "自动同步工控机";
		ControlExpendBatch erpBatch = new ControlExpendBatch();
		erpBatch.setLineId(lineId);
		erpBatch.setErpInvoiceId(erpInvId);
		erpBatch.setBatchStere(pieAmnt);
		erpBatch.setBatchTime(datTim);
		erpBatch.setConBatchNo(pieId);
		erpBatch.setBatchNum(serial);
		erpBatch.setType(autoType);
		erpBatch.setRemark(remark);
		erpBatch.setStatus(StateEnum.ENABLED.getCode());
		erpBatch.setCreator(bcsCreater);
		erpBatch.setCreateTime(createDate);
		return erpBatch;
	}
}
